﻿@model EntityField
@using NewLife;
@using Microsoft.AspNetCore.Routing
@using XCode;
@using XCode.Configuration;
@{
    var entity = Model.Entity;
    var item = Model.Field;
    var factory = entity.GetType().AsFactory();
    var page = ViewBag.Page as Pager;

    var value = entity[item.Name];
    var provider = ManageProvider.Provider;
    var listField = item as ListField;

    // 第二名称，去掉后面的数字，便于模式匹配
    var digits = new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    var name2 = item.Name.TrimEnd(digits);

    // 文本对齐方式
    var tdClass = listField.GetTextClass(entity);
    var title = listField.GetTitle(entity, page);
}
@if (item.PrimaryKey)
{
    <td class="@(tdClass ?? "text-center")" title="@title">@value</td>
}
else
{
    switch (Type.GetTypeCode(item.Type))
    {
        case TypeCode.Boolean:
            var bval = (Boolean)value;
            <td class="@(tdClass ?? "text-center")" title="@title">
                @if (item.Name.EqualIgnoreCase("Enable") && factory.Unique != null)
                {
                    var key = entity[factory.Unique.Name];
                    <a href="@Url.Action("SetEnable", null, new { id = key, enable = !bval })" data-action="action">
                        @await Html.PartialAsync("_Icon_Boolean", bval)
                    </a>
                }
                else
                {
                    @await Html.PartialAsync("_Icon_Boolean", bval)
                }
            </td>
            break;
        case TypeCode.DateTime:
            var dt = (DateTime)value;
            if (listField.GetValue != null)
            {
                <td class="@(tdClass ?? "text-center")" title="@title">@listField.GetValue(entity)</td>
            }
            else if (item.ItemType.EqualIgnoreCase("time"))
            {
                <td class="@(tdClass ?? "text-center")" title="@title">@dt.ToFullString("")</td>
            }
            else if (item.ItemType.StartsWithIgnoreCase("time-"))
            {
                var format = item.ItemType.Substring("time-".Length);
                <td class="@(tdClass ?? "text-center")" title="@title">@dt.ToString(format, "")</td>
            }
            else if (item.ItemType.EqualIgnoreCase("Date") || item.ItemType.IsNullOrEmpty() && name2.EndsWith("Date"))
            {
                <td class="@(tdClass ?? "text-center")" title="@title">@dt.ToString("yyyy-MM-dd", "")</td>
            }
            else
            {
                <td class="@(tdClass ?? "text-center")" title="@title">@dt.ToFullString("")</td>
            }
            break;
        case TypeCode.Decimal:
            if (listField.GetValue != null)
            {
                <td class="@(tdClass ?? "text-center")" title="@title">@listField.GetValue(entity)</td>
            }
            else if (item.ItemType.EqualIgnoreCase("Money"))
            {
                var dc = (Decimal)value;
                <td class="@(tdClass ?? "text-right")" title="@title">@dc.ToString("c")</td>
            }
            else
            {
                var dc = (Decimal)value;
                if (item.Scale > 0)
                {
                    <td class="@(tdClass ?? "text-right")" title="@title">@dc.ToString("n" + item.Scale)</td>
                }
                else
                {
                    <td class="@(tdClass ?? "text-right")" title="@title">@dc.ToString()</td>
                }
            }
            break;
        case TypeCode.Single:
        case TypeCode.Double:
            if (listField.GetValue != null)
            {
                <td class="@(tdClass ?? "text-right")" title="@title">@listField.GetValue(entity)</td>
            }
            else if (item.ItemType.EqualIgnoreCase("percent", "Percentage"))
            {
                var dd = value.ToDouble();
                var scale = item.Scale >= 2 ? item.Scale - 2 : 2;
                <td class="@(tdClass ?? "text-center")" title="@title">@dd.ToString("p" + scale)</td>
            }
            else if (item.ItemType.EqualIgnoreCase("percent", "Percentage"))
            {
                var dd = value.ToDouble();
                var scale = 0;
                var des = item.Description + "";
                if (des.Contains("十分之一")) { dd /= 10; scale += 1; }
                if (des.Contains("百分之一")) { dd /= 100; scale += 2; }
                if (des.Contains("千分之一")) { dd /= 1000; scale += 3; }
                if (des.Contains("万分之一")) { dd /= 10000; scale += 4; }
                scale = scale >= 2 ? scale - 2 : 2;

                <td class="@(tdClass ?? "text-center")" title="@title">@dd.ToString("p" + scale)</td>
            }
            else
            {
                var dd = value.ToDouble();
                if (item.Scale > 0)
                {
                    <td class="@(tdClass ?? "text-right")" title="@title">@dd.ToString("n" + item.Scale)</td>
                }
                else
                {
                    <td class="@(tdClass ?? "text-right")" title="@title">@dd.ToString()</td>
                }
            }
            break;
        case TypeCode.Byte:
        case TypeCode.Int16:
        case TypeCode.Int32:
        case TypeCode.Int64:
        case TypeCode.UInt16:
        case TypeCode.UInt32:
        case TypeCode.UInt64:
            if (listField.GetValue != null)
            {
                <td class="@(tdClass ?? "text-right")" title="@title">@listField.GetValue(entity)</td>
            }
            //特殊处理枚举
            else if (item.Type.IsEnum)
            {
                var val = "";
                try
                {
                    val = EnumHelper.GetDescription((Enum)value) ?? value + "";
                }
                catch { }
                <td class="@(tdClass ?? "text-center")" title="@title">@val</td>
            }
            else if (item.ItemType.StartsWithIgnoreCase("TimeSpan"))
            {
                <td class="@(tdClass ?? "text-right")" title="@title">@(item.FormatValue(value))</td>
            }
            else if (item.ItemType.StartsWithIgnoreCase("Time"))
            {
                <td class="@(tdClass ?? "text-center")" title="@title">@(item.FormatValue(value))</td>
            }
            else if (item.ItemType.EqualIgnoreCase("GMK"))
            {
                <td class="@(tdClass ?? "text-right")" title="@title">@(value.ToLong().ToGMK())</td>
            }
            else if (item.Name.EqualIgnoreCase("CreateUserID", "UpdateUserID"))
            {
                //var provider = ViewData["Provider"] as IManageProvider;
                if (provider != null)
                {
                    <td class="@(tdClass ?? "text-center")" title="@title">@provider?.FindByID(value)</td>
                }
                else
                {
                    <td class="@(tdClass ?? "text-center")" title="@title">@value</td>
                }
            }
            else if (item.ItemType.EqualIgnoreCase("percent", "Percentage"))
            {
                var dd = Convert.ToDouble(value);
                var scale = 0;
                var des = item.Description + "";
                if (des.Contains("十分之一")) { dd /= 10; scale = 0; }
                if (des.Contains("百分之一")) { dd /= 100; scale = 0; }
                if (des.Contains("千分之一")) { dd /= 1000; scale = 1; }
                if (des.Contains("万分之一")) { dd /= 10000; scale = 2; }

                <td class="@(tdClass ?? "text-center")" title="@title">@dd.ToString("p" + scale)</td>
            }
            else
            {
                <td class="@(tdClass ?? "text-right")" title="@title">@Convert.ToInt64(value).ToString("n0")</td>
            }
            break;
        case TypeCode.String:
        default:
            if (listField.GetValue != null)
            {
                <td class="@tdClass" title="@title">@listField.GetValue(entity)</td>
            }
            else
            {
                var val = value;
                if (!item.MapField.IsNullOrEmpty() && val + "" != "")
                {
                    if (item.MapProvider != null)
                    {
                        var name = item.MapProvider.EntityType.Name;
                        var key = item.MapProvider.Key;
                        val = entity[item.MapField];
                        <td class="@(tdClass ?? "text-center")" title="@title"><a href="@name?@key=@val">@value</a></td>
                    }
                    else
                    {
                        <td class="@(tdClass ?? "text-center")" title="@title">@value</td>
                    }
                }
                else if (item.Name.EqualIgnoreCase("CreateIP", "UpdateIP"))
                {
                    <td class="@(tdClass ?? "text-center")" title="@((value + "").IPToAddress())">@value</td>
                }
                else if (item.IsBigText())
                {
                    // 防止内容过长
                    if (!tdClass.IsNullOrEmpty())
                    {
                        <td style="@tdClass" title="@value">@value</td>
                    }
                    else
                    {
                        <td style="max-width:600px;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;" title="@value">@value</td>
                    }
                }
                else
                {
                    <td class="@tdClass" title="@title">@value</td>
                }
            }
            break;
    }
}